﻿Imports TNCDAL
Imports System.Linq

Public Class ucVoyage

    Dim db As New TanNamChinhDataContext
    Dim ID_Vessel As Integer = 0
    Dim ID_Currency As Integer = 0
    Dim dtBangCuoc As New DataTable
    Dim m_vessel As New CVessel
    Dim c_FT As Integer = 0 ' dem so lan goi Fei Tariff

    Public Sub KhoiTaocbb()
        Dim m_Line As New CLine
        Dim line = m_Line.SelectAllRecord
        cbbLine.DataSource = line
        cbbLine.DisplayMember = "LineName"
        cbbLine.ValueMember = "ID_Line"

        Dim currency = From p In db.tblCurrencies
                       Select p

        cbbCurrency.DataSource = currency
        cbbCurrency.DisplayMember = "Name"
        cbbCurrency.ValueMember = "ID_Currency"
    End Sub

    Public Sub KhoiTaocbbVessel(ByVal ID_Line As Integer)
        Dim m_Vessel As New CVessel
        cbbVessel.DataSource = m_Vessel.SelectAllRecord(ID_Line)
        cbbVessel.DisplayMember = "VesselName"
        cbbVessel.ValueMember = "ID_Vessel"
    End Sub

    Private Sub ucVoyage_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        KhoiTaocbb()

        KhoiTaoThuocTinhlvContType()
        KhoiTaolvContType()
        KhoiTaoThuocTinhlvFee()
        KhoiTaolvFee()

        KhoiTaocbbPOL_POD()
        'KhoiTaodtgVoyage()
        KhoiTao_GiaoDien(True)
        pnlContType.Visible = False
        pnlFee.Visible = False
    End Sub

    Public Sub KhoiTaocbbPOL_POD()
        Dim port = (From q In db.tblPorts
                   Select q Order By q.Port)

        Dim p_All As New tblPort
        p_All.ID_Port = 0
        p_All.Port = "TẤT CẢ"

        Dim listPOL As New List(Of tblPort)
        Dim listPOD As New List(Of tblPort)
        Dim listPOL_TK As New List(Of tblPort)
        Dim listPOD_TK As New List(Of tblPort)
        listPOL_TK.Add(p_All)
        listPOD_TK.Add(p_All)

        For Each p As tblPort In port
            listPOL.Add(p)
            listPOD.Add(p)
            listPOL_TK.Add(p)
            listPOD_TK.Add(p)
        Next

        cbbPOD_TK.DataSource = listPOD_TK
        cbbPOD_TK.DisplayMember = "Port"
        cbbPOD_TK.ValueMember = "ID_Port"


        cbbPOL_TK.DataSource = listPOL_TK
        cbbPOL_TK.DisplayMember = "Port"
        cbbPOL_TK.ValueMember = "ID_Port"

        cbbPOD.DataSource = listPOD
        cbbPOD.DisplayMember = "Port"
        cbbPOD.ValueMember = "ID_Port"


        cbbPOL.DataSource = listPOL
        cbbPOL.DisplayMember = "Port"
        cbbPOL.ValueMember = "ID_Port"

        cbbPOD_TK.SelectedIndex = 0
        cbbPOL_TK.SelectedIndex = 0
    End Sub

    Public Sub KhoiTaoThuocTinhlvFee()
        lvFee.View = View.Details
        lvFee.BackColor = Color.White
        lvFee.FullRowSelect = True
        lvFee.Columns.Add("Loại phí", 180)
    End Sub

    Public Sub KhoiTaolvFee()
        Dim fee = From p In db.tblFees
                   Where p.ID_FeeType = 1
                   Select p

        lvFee.Items.Clear()

        For Each p As tblFee In fee
            Dim item As New ListViewItem
            item.Text = p.FeeName
            item.Name = p.ID_Fee
            lvFee.Items.Add(item)
        Next
    End Sub

    Public Sub KhoiTaoThuocTinhlvContType()
        lvContType.View = View.Details
        lvContType.BackColor = Color.White
        lvContType.FullRowSelect = True
        lvContType.Columns.Add("Loại Cont", 180)
    End Sub

    Public Sub KhoiTaolvContType()
        Dim agent = From p In db.tblContTypes
           Select p Order By p.ContTypeName Ascending


        lvContType.Items.Clear()

        For Each p As tblContType In agent
            Dim item As New ListViewItem
            item.Text = p.ContTypeName
            item.Name = p.ID_ContType
            lvContType.Items.Add(item)
        Next
    End Sub

    Public Sub KhoiTaodtgVoyage()
        Dim IDPOL As Integer = 0
        Dim IDPOD As Integer = 0

        Try
            IDPOL = cbbPOL_TK.SelectedValue
            IDPOD = cbbPOD_TK.SelectedValue
        Catch ex As Exception

        End Try

        If IDPOL = 0 And IDPOD = 0 Then
            Dim voyage = From p In db.tblVoyages
                    Select p.ID_Voyage, p.tblLine.ID_Line, p.tblLine.LineName, p.tblVessel.VesselName, p.VoyageNo, POL = p.tblPort.Port, POD = p.tblPort1.Port, p.ETD, p.ETA, ID_POL = p.tblPort.ID_Port, ID_POD = p.tblPort1.ID_Port, p.ID_Vessel
                    Order By ID_Voyage Descending

            dtgVoyage.DataSource = voyage
        ElseIf IDPOL = 0 And IDPOD > 0 Then
            Dim voyage = From p In db.tblVoyages
                    Where p.ID_POD = IDPOD
                    Select p.ID_Voyage, p.tblLine.ID_Line, p.tblLine.LineName, p.tblVessel.VesselName, p.VoyageNo, POL = p.tblPort.Port, POD = p.tblPort1.Port, p.ETD, p.ETA, ID_POL = p.tblPort.ID_Port, ID_POD = p.tblPort1.ID_Port, p.ID_Vessel
                    Order By ID_Voyage Descending

            dtgVoyage.DataSource = voyage
        ElseIf IDPOL > 0 And IDPOD = 0 Then
            Dim voyage = From p In db.tblVoyages
                    Where p.ID_POL = IDPOL
                    Select p.ID_Voyage, p.tblLine.ID_Line, p.tblLine.LineName, p.tblVessel.VesselName, p.VoyageNo, POL = p.tblPort.Port, POD = p.tblPort1.Port, p.ETD, p.ETA, ID_POL = p.tblPort.ID_Port, ID_POD = p.tblPort1.ID_Port, p.ID_Vessel
                    Order By ID_Voyage Descending

            dtgVoyage.DataSource = voyage
        Else
            Dim voyage = From p In db.tblVoyages
                    Where p.ID_POD = IDPOD And p.ID_POL = IDPOL
                    Select p.ID_Voyage, p.tblLine.ID_Line, p.tblLine.LineName, p.tblVessel.VesselName, p.VoyageNo, POL = p.tblPort.Port, POD = p.tblPort1.Port, p.ETD, p.ETA, ID_POL = p.tblPort.ID_Port, ID_POD = p.tblPort1.ID_Port, p.ID_Vessel
                    Order By ID_Voyage Descending

            dtgVoyage.DataSource = voyage
        End If

        FormatdtgVoyage()
    End Sub

    Public Sub FormatdtgVoyage()
        dtgVoyage.RowHeadersWidth = 20
        dtgVoyage.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        'dtgVoyage.RowTemplate.Height = 25

        dtgVoyage.MultiSelect = False
        dtgVoyage.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgVoyage.ReadOnly = True
        dtgVoyage.BackgroundColor = Color.White
        dtgVoyage.AllowUserToAddRows = False
        dtgVoyage.AllowUserToDeleteRows = False
        dtgVoyage.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgVoyage.Columns("ID_Voyage").Visible = False
        dtgVoyage.Columns("ETA").Visible = False
        dtgVoyage.Columns("ETD").Visible = False
        dtgVoyage.Columns("ID_POL").Visible = False
        dtgVoyage.Columns("ID_POD").Visible = False
        dtgVoyage.Columns("ID_Vessel").Visible = False
        dtgVoyage.Columns("ID_Currency").Visible = False
        dtgVoyage.Columns("ID_Line").Visible = False
        With dtgVoyage.Columns("LineName")
            .HeaderText = "Line"
            .Width = 80
        End With
        With dtgVoyage.Columns("VesselName")
            .HeaderText = "Vessel"
            .Width = 120
        End With
        With dtgVoyage.Columns("VoyageNo")
            .HeaderText = "Voyage"
            .Width = 80
        End With
        With dtgVoyage.Columns("POL")
            .HeaderText = "POL"
            .Width = 100
        End With
        With dtgVoyage.Columns("POD")
            .HeaderText = "POD"
            .Width = 100
        End With
    End Sub

    Private Sub dtgVoyage_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgVoyage.CurrentCellChanged
        Try
            Dim ID_Voyage As Integer
            ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value
            c_FT += 1
            If c_FT > 4 Then
                KhoiTaodtgFreightTariff(ID_Voyage)
            End If


            cbbPOD.SelectedValue = dtgVoyage.CurrentRow.Cells("ID_POD").Value
            cbbPOL.SelectedValue = dtgVoyage.CurrentRow.Cells("ID_POL").Value
            txtVoyageNo.Text = dtgVoyage.CurrentRow.Cells("VoyageNo").Value

            Try
                dtpETA.Value = dtgVoyage.CurrentRow.Cells("ETA").Value
                dtpETATime.Value = CDate(dtgVoyage.CurrentRow.Cells("ETA").Value).ToShortTimeString
                dtpETD.Value = dtgVoyage.CurrentRow.Cells("ETD").Value
                dtpETDTime.Value = CDate(dtgVoyage.CurrentRow.Cells("ETD").Value).ToShortTimeString
            Catch ex As Exception

            End Try
            
            Try
                Dim ID_Line As Integer = dtgVoyage.CurrentRow.Cells("ID_Line").Value
                cbbLine.SelectedValue = ID_Line
                KhoiTaocbbVessel(ID_Line)
                cbbVessel.SelectedValue = dtgVoyage.CurrentRow.Cells("ID_Vessel").Value
            Catch ex As Exception

            End Try

            cbbCurrency.SelectedValue = dtgVoyage.CurrentRow.Cells("ID_Currency").Value


            ID_Vessel = dtgVoyage.CurrentRow.Cells("ID_Vessel").Value
            ID_Currency = dtgVoyage.CurrentRow.Cells("ID_Currency").Value

        Catch ex As Exception
            'MsgBox(ex.Message)
        End Try
    End Sub


#Region "Them, Xoa, Sua Voyage"
    Dim fV_Them As Boolean = False
    Dim fV_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        fV_Them = True
        ClearVoyage()
        KhoiTao_GiaoDien(False)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        fV_Sua = True
        KhoiTao_GiaoDien(False)
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        'sy
        'chua kiem tra rang buoc du lieu

        Dim VoyageNo As String = ""
        VoyageNo = dtgVoyage.CurrentRow.Cells("VoyageNo").Value

        If MsgBox("Bạn muốn xóa chuyến tàu số: " + VoyageNo, MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes Then
            Dim ID_Voyage As Integer
            ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

            Dim ft = From p In db.tblFreightTariffs
                     Where p.ID_Voyage = ID_Voyage
                     Select p

            db.tblFreightTariffs.DeleteAllOnSubmit(ft)

            Dim voyage = From p In db.tblVoyages
                         Where p.ID_Voyage = ID_Voyage
                         Select p

            db.tblVoyages.DeleteAllOnSubmit(voyage)

            db.SubmitChanges()
            'Refesh
            KhoiTaodtgVoyage()
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'Kiem tra du lieu
        If cbbPOD.SelectedIndex = -1 Or cbbPOL.SelectedIndex = -1 Then
            MsgBox("Bạn chưa chọn Cảng đi hoặc Cảng đến")
            cbbPOL.Focus()
            Exit Sub
        End If

        ID_Vessel = cbbVessel.SelectedValue
        If ID_Vessel = 0 Then
            MsgBox("Bạn chưa chọn Hãng tàu")
            cbbLine.Focus()
            Exit Sub
        End If

        ID_Currency = cbbCurrency.SelectedValue
        If ID_Currency = 0 Then
            MsgBox("Bạn chưa chọn ngoại tệ, tỷ giá cho bảng cước vận chuyển")
            cbbCurrency.Focus()
            Exit Sub
        End If


        If fV_Them Then
            Dim voyage As New tblVoyage
            voyage.ID_Vessel = ID_Vessel

            If dtpETATime.Text <> "" Then
                voyage.ETA = dtpETA.Value.ToShortDateString + " " + dtpETATime.Value.ToShortTimeString
            Else
                voyage.ETA = dtpETA.Value.ToShortDateString + " 00:00:00 AM"
            End If

            If dtpETDTime.Text <> "" Then
                voyage.ETD = dtpETD.Value.ToShortDateString + " " + dtpETDTime.Value.ToShortTimeString
            Else
                voyage.ETD = dtpETD.Value.ToShortDateString + " 00:00:00 AM"
            End If
            voyage.ID_POD = cbbPOD.SelectedValue
            voyage.ID_POL = cbbPOL.SelectedValue
            voyage.VoyageNo = txtVoyageNo.Text

            db.tblVoyages.InsertOnSubmit(voyage)
            db.SubmitChanges()
        ElseIf fV_Sua Then
            Dim ID_Voyage As Integer
            ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value
            Dim voyage = (From p In db.tblVoyages
                         Where p.ID_Voyage = ID_Voyage
                         Select p).Single

            voyage.ID_Vessel = ID_Vessel

            If dtpETATime.Text <> "" Then
                voyage.ETA = dtpETA.Value.ToShortDateString + " " + dtpETATime.Value.ToShortTimeString
            Else
                voyage.ETA = dtpETA.Value.ToShortDateString + " 00:00:00 AM"
            End If

            If dtpETDTime.Text <> "" Then
                voyage.ETD = dtpETD.Value.ToShortDateString + " " + dtpETDTime.Value.ToShortTimeString
            Else
                voyage.ETD = dtpETD.Value.ToShortDateString + " 00:00:00 AM"
            End If


            voyage.ID_POD = cbbPOD.SelectedValue
            voyage.ID_POL = cbbPOL.SelectedValue
            voyage.VoyageNo = txtVoyageNo.Text
            db.SubmitChanges()
        End If

        fV_Them = False
        fV_Sua = False
        KhoiTaodtgVoyage()
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        fV_Them = False
        fV_Sua = False
        'Refesh
        KhoiTaodtgVoyage()
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
        'Copy Voyage and FreightTariff
        Dim voyage As New tblVoyage
        voyage.ID_Vessel = dtgVoyage.CurrentRow.Cells("ID_Vessel").Value
        voyage.ETA = dtgVoyage.CurrentRow.Cells("ETA").Value
        voyage.ETD = dtgVoyage.CurrentRow.Cells("ETD").Value
        voyage.ID_POD = dtgVoyage.CurrentRow.Cells("ID_POD").Value
        voyage.ID_POL = dtgVoyage.CurrentRow.Cells("ID_POL").Value
        voyage.VoyageNo = "" 'dtgVoyage.CurrentRow.Cells("VoyageNo").Value

        db.tblVoyages.InsertOnSubmit(voyage)
        db.SubmitChanges()

        Dim ID_Voyage As Integer
        ID_Voyage = voyage.ID_Voyage

        Dim ID_Voyage_Copy As Integer = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

        Dim ft = From p In db.tblFreightTariffs
                  Where p.ID_Voyage = ID_Voyage_Copy
                  Select p

        For Each freightTariff In ft
            Dim new_ft As New tblFreightTariff
            new_ft.ID_Voyage = ID_Voyage
            new_ft.ID_Fee = freightTariff.ID_Fee
            new_ft.ID_ContType = freightTariff.ID_ContType
            new_ft.Price = freightTariff.Price
            db.tblFreightTariffs.InsertOnSubmit(new_ft)
        Next
        db.SubmitChanges()

        KhoiTaodtgVoyage()
        Try
            dtgVoyage.Rows(0).Selected = True
            dtgVoyage.SelectedCells(2).Selected = True
            btnEdit_Click(Nothing, Nothing)
        Catch ex As Exception

        End Try

    End Sub

    Public Sub ClearVoyage()
        txtVoyageNo.Text = ""
        cbbLine.SelectedIndex = 0

        cbbPOL.SelectedIndex = -1
        cbbPOD.SelectedIndex = -1
        dtpETA.Value = Now.Date
        dtpETD.Value = Now.Date
        cbbCurrency.SelectedIndex = 0
        ID_Vessel = 0
        ID_Currency = 0
        dtgFreightTariff.DataSource = Nothing
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        'set ReadOnly,visible,enable
        dtgVoyage.Enabled = flag

        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        btnCopy.Enabled = flag


        txtVoyageNo.ReadOnly = flag
        dtpETA.Enabled = Not flag
        dtpETD.Enabled = Not flag
        dtpETATime.Enabled = Not flag
        dtpETDTime.Enabled = Not flag

        cbbPOD.Enabled = Not flag
        cbbPOL.Enabled = Not flag
        cbbLine.Enabled = Not flag
        cbbVessel.Enabled = Not flag
        cbbCurrency.Enabled = Not flag

        'set backcolor
        If (flag) Then
            txtVoyageNo.BackColor = Color.Azure
        Else
            txtVoyageNo.BackColor = Color.White
        End If
    End Sub
#End Region

#Region "FreightTariff"
    Public Sub KhoiTaodtgFreightTariff(ByVal ID_Voyage As Integer)
        Try
            dtBangCuoc = GetBangCuoc(ID_Voyage)
            dtgFreightTariff.DataSource = dtBangCuoc
            FormatdtgFreightTariff()


            Dim dys = Enumerable.Range(1, db.tblFreightTariffs.Max(Function(p) p.ID_Fee))

            'dys.Select()
            Dim grps = From p In db.tblFreightTariffs
                       Where p.ID_Voyage = ID_Voyage
                       Group By Key = New With {p.ID_ContType} Into g = Group
                       Let lookup = g.ToLookup(Function(x) x.ID_Fee, Function(x) x.Price)
            'Select Key.ID_ContType, abc = dys.Select(Function(x) lookup(x).Sum()).ToArray

            dtgFreightTariff.DataSource = grps

        Catch ex As Exception
            MsgBox("Khởi tạo bảng cước vận tải: " + ex.Message)
        End Try
    End Sub

    Public Sub FormatdtgFreightTariff()
        dtgFreightTariff.RowHeadersWidth = 20
        dtgFreightTariff.RowHeadersDefaultCellStyle.ForeColor = Color.Blue
        dtgFreightTariff.RowTemplate.Height = 25

        dtgFreightTariff.MultiSelect = False
        'dtgFreightTariff.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgFreightTariff.BackgroundColor = Color.White
        dtgFreightTariff.AllowUserToAddRows = False
        dtgFreightTariff.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgFreightTariff.Columns("ID_ContType").Visible = False
        With dtgFreightTariff.Columns("ContTypeName")
            .HeaderText = "Loại Cont"
            .Width = 100
            .ReadOnly = True
        End With

        For i As Integer = 2 To dtBangCuoc.Columns.Count - 1
            With dtgFreightTariff.Columns(i)
                .DefaultCellStyle.Format = "#,###"
                .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                .Width = 60
            End With
        Next
    End Sub

    Public Function GetBangCuoc(ByVal ID_Voyage As Integer) As DataTable
        Dim listFee = (From p In db.tblFreightTariffs
                Where p.ID_Voyage = ID_Voyage
                Select ID_Fee = p.ID_Fee).Distinct.ToList

        Dim listContType = (From p In db.tblFreightTariffs
                Where p.ID_Voyage = ID_Voyage
                Select ID_ContType = p.ID_ContType).Distinct.ToList

        Dim contType = From p In db.tblContTypes
                       Where listContType.Contains(p.ID_ContType)
                       Select p

        Dim fee = From p In db.tblFees
                  Where listFee.Contains(p.ID_Fee)
                  Select p


        Dim dt As New DataTable
        'Row la loai container
        'Col la cac loai phi cua hang tau theo tung loai cont

        Dim column As DataColumn
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.Int32")
        column.ColumnName = "ID_ContType"

        ' Add the Column to the DataColumnCollection.
        dt.Columns.Add(column)

        ' Create second column.
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = "ContTypeName"

        ' Add the column to the table.
        dt.Columns.Add(column)

        'Kiem tra da co du lieu chua?
        If fee.Count = 0 Then
            Return dt
        End If

        For Each f As tblFee In fee
            Dim feeName As String
            Dim ID_Fee As Integer

            feeName = f.FeeName
            ID_Fee = f.ID_Fee

            column = New DataColumn()
            column.DataType = System.Type.GetType("System.Double")
            column.ColumnName = feeName
            column.Caption = ID_Fee
            dt.Columns.Add(column)
        Next

        For Each ct As tblContType In contType
            Dim row As DataRow
            row = dt.NewRow
            row(0) = ct.ID_ContType
            row(1) = ct.ContTypeName

            For i As Integer = 2 To dt.Columns.Count - 1
                row(i) = GetPrice(ID_Voyage, ct.ID_ContType, CInt(dt.Columns(i).Caption))
            Next
            dt.Rows.Add(row)
        Next

        Return dt
    End Function

    Public Function GetPrice(ByVal ID_Voyage As Integer, ByVal ID_ContType As Integer, ByVal ID_Fee As Integer) As Double
        Dim kq As Double = 0
        Dim ft = (From p In db.tblFreightTariffs
                    Where p.ID_Voyage = ID_Voyage And p.ID_ContType = ID_ContType And p.ID_Fee = ID_Fee
                    Select p).Single
        kq = ft.Price
        Return kq
    End Function

    'Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
    '    'Them Bang cuoc van chuyen
    '    Dim fee = From p In db.tblFees
    '              Where p.ID_FeeType = 1
    '              Select p

    '    Dim contType = From p In db.tblContTypes
    '                   Select p

    '    Dim ID_Voyage As Integer
    '    ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

    '    For Each ct As tblContType In contType
    '        Dim ID_ContType As Integer
    '        ID_ContType = ct.ID_ContType
    '        For Each f As tblFee In fee
    '            'insert into tblFreightTariff
    '            Dim ID_Fee As Integer
    '            ID_Fee = f.ID_Fee

    '            Dim ft As New tblFreightTariff
    '            ft.ID_Voyage = ID_Voyage
    '            ft.ID_ContType = ID_ContType
    '            ft.ID_Fee = ID_Fee
    '            ft.Price = 0
    '            db.tblFreightTariffs.InsertOnSubmit(ft)
    '            db.SubmitChanges()
    '        Next
    '    Next
    '    MsgBox("ok")
    '    'Refesh dtgGiaCuoc
    'End Sub
#End Region

    Private Sub lvContType_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvContType.DoubleClick
        Dim contTypeName As String
        Dim ID_ContType As Integer
        contTypeName = lvContType.SelectedItems(0).Text
        ID_ContType = lvContType.SelectedItems(0).Name

        'Kiểm tra tồn tại row
        For i As Integer = 0 To dtBangCuoc.Rows.Count - 1
            If dtBangCuoc.Rows(i).Item("ID_ContType") = ID_ContType Then
                MsgBox("Đã có loại cont " + contTypeName + " trong bảng cước.")
                Exit Sub
            End If
        Next

        Dim row As DataRow
        row = dtBangCuoc.NewRow
        row(0) = ID_ContType
        row(1) = contTypeName

        For i As Integer = 2 To dtBangCuoc.Columns.Count - 1
            row(i) = 0
        Next
        dtBangCuoc.Rows.Add(row)
    End Sub


    Private Sub lvFee_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvFee.DoubleClick
        Dim feeName As String
        Dim ID_Fee As Integer

        feeName = lvFee.SelectedItems(0).Text
        ID_Fee = lvFee.SelectedItems(0).Name

        'Kiểm tra tồn tại col
        For i As Integer = 2 To dtBangCuoc.Columns.Count - 1
            If CInt(dtBangCuoc.Columns(i).Caption) = ID_Fee Then
                MsgBox("Đã có " + feeName + " trong bảng cước.")
                Exit Sub
            End If
        Next

        Dim column As DataColumn
        column = New DataColumn()
        column.DataType = System.Type.GetType("System.Double")
        column.ColumnName = feeName
        column.Caption = ID_Fee

        dtBangCuoc.Columns.Add(column)

        Dim ID_Voyage As Integer
        ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value
    End Sub

    Private Sub btnGhi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGhi.Click
        Dim ID_Voyage As Integer
        ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

        '1. Xoa het bang cuoc
        Dim ft = From p In db.tblFreightTariffs
                 Where p.ID_Voyage = ID_Voyage
                 Select p
        db.tblFreightTariffs.DeleteAllOnSubmit(ft)
        db.SubmitChanges()

        '2. Them moi bang cuoc
        For Each row As DataRow In dtBangCuoc.Rows
            Dim ID_ContType As Integer
            ID_ContType = row.Item("ID_ContType")
            For i As Integer = 2 To dtBangCuoc.Columns.Count - 1
                Dim ID_Fee As Integer
                ID_Fee = dtBangCuoc.Columns(i).Caption
                Dim price As Double = 0
                Try
                    price = row.Item(i)
                Catch ex As Exception

                End Try
                'MsgBox("Bo 3: ID_Voyage=" & ID_Voyage & " ID_ContType=" & ID_ContType & " ID_Fee=" & ID_Fee)
                Dim f As New tblFreightTariff
                f.ID_Voyage = ID_Voyage
                f.ID_ContType = ID_ContType
                f.ID_Fee = ID_Fee
                f.Price = price
                db.tblFreightTariffs.InsertOnSubmit(f)
                db.SubmitChanges()
            Next
        Next

        MsgBox("Đã lưu dữ liệu")
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim ID_Voyage As Integer
        ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value

        Dim ft = From p In db.tblFreightTariffs
                 Where p.ID_Voyage = ID_Voyage
                 Select p
        db.tblFreightTariffs.DeleteAllOnSubmit(ft)
        db.SubmitChanges()

        KhoiTaodtgFreightTariff(ID_Voyage)
    End Sub

    Private Sub dtgFreightTariff_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) Handles dtgFreightTariff.UserDeletingRow
        If (MsgBox("Bạn muốn xóa loại cont này?", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Dim ID_Voyage As Integer
            ID_Voyage = dtgVoyage.CurrentRow.Cells("ID_Voyage").Value
            Dim ID_ContType As Integer
            ID_ContType = dtgFreightTariff.CurrentRow.Cells("ID_ContType").Value

            Dim ft = From p In db.tblFreightTariffs
                     Where p.ID_Voyage = ID_Voyage And p.ID_ContType = ID_ContType
                     Select p
            db.tblFreightTariffs.DeleteAllOnSubmit(ft)
            db.SubmitChanges()

            'KhoiTaodtgFreightTariff(ID_Voyage)
        Else
            e.Cancel = True
        End If
    End Sub

    Private Sub cbbPOD_TK_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbPOD_TK.SelectedIndexChanged
        Try
            KhoiTaodtgVoyage()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub cbbPOL_TK_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbPOL_TK.SelectedIndexChanged
        Try
            KhoiTaodtgVoyage()
        Catch ex As Exception

        End Try
    End Sub

    
    Private Sub cbbLine_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbbLine.SelectedIndexChanged
        Try
            Dim ID_Line As Integer
            ID_Line = cbbLine.SelectedValue
            KhoiTaocbbVessel(ID_Line)
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnContType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnContType.Click
        pnlContType.Visible = Not pnlContType.Visible
    End Sub

    Private Sub btnFee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFee.Click
        pnlFee.Visible = Not pnlFee.Visible
    End Sub
    
    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        'CModule_Sy.ChuyenSoLieuTuExcel()
    End Sub
End Class

